/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "dlna_listener_impl.h"
#include "dlna_stream_manager.h"
#include "cast_engine_log.h"



namespace OHOS {
namespace CastEngine {
namespace CastEngineService {
DEFINE_CAST_ENGINE_LABEL("Dlna-Listener-Impl");
DlnaListenerImpl::DlnaListenerImpl(wptr<CastEngineDlnaService> castEngineDlnaService)
    :castEngineDlnaService_(castEngineDlnaService)
{

}


DlnaListenerImpl::~DlnaListenerImpl()
{

}

bool DlnaListenerImpl::OnPlay()
{
    delayStop_.Stop();
    return true;
}

bool DlnaListenerImpl::OnPlayerReady(const MediaUrlData &mediaUrlData)
{
    CLOGI("OnPlayerReady");

    delayStop_.Stop();
    
    int ret = castEngineDlnaService_->OnPlayerReady(mediaUrlData.clientIP);
    if(ret >= 0) {
        return true;
    }
    return false;
}

bool DlnaListenerImpl::OnPlayerEnd()
{
    CLOGI("OnPlayerEnd");

    CLOGI("start stop player");
    delayStop_.Start([this](){
        CLOGI("stop player");
        castEngineDlnaService_->OnPlayerEnd();
    },3000,true);
    
    return true;
}

bool DlnaListenerImpl::OnPhotoReady(const MediaUrlData &mediaUrlData)
{
    CLOGI("DlnaListenerImpl::OnPhotoReady photoUrl:%{public}s",mediaUrlData.pucUri);
    delayStop_.Stop();
    castEngineDlnaService_->OnPhotoReady((const char*)mediaUrlData.pucUri);
    return true;
}

void DlnaListenerImpl::NotifyEventChange(int moduleId, int event, const std::string &param)  
{
    CLOGI("ProcessActionsEvent param  %{public}s",param.c_str());
    DlnaStreamManager::GetInstance()->ProcessActionsEvent(event, param);
}   

bool DlnaListenerImpl::NotifyEvent(int event)  
{
    return true;
}
} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS